﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/
namespace Pes.DBFactory
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Globalization;
    using System.Linq;
    using System.Text;

    public static class DBOperations
    {
        public static bool ExecuteNonQuery(SqlCommand msCommand)
        {
            if (msCommand != null)
            {
                using (SqlConnection msConnection = new SqlConnection(Pes.DBFactory.DBConnection.ConnectionString()))
                {
                    msCommand.Connection = msConnection;
                    msConnection.Open();
                    msCommand.ExecuteNonQuery();
                    return true;
                }
            }

            return false;
        }

        public static int ExecuteNonQuery(SqlCommand msCommand, string outParameter)
        {
            if (msCommand != null)
            {
                using (SqlConnection msConnection = new SqlConnection(Pes.DBFactory.DBConnection.ConnectionString()))
                {
                    msCommand.Connection = msConnection;
                    msConnection.Open();
                    msCommand.ExecuteNonQuery();
                    int outId = Pes.Utility.Conversion.TryCastInteger(msCommand.Parameters[outParameter].Value);
                    return outId;
                }
            }

            return 0;
        }


        public static object GetScalarValue(SqlCommand command)
        {
            if (command != null)
            {
                using (SqlConnection connection = new SqlConnection(Pes.DBFactory.DBConnection.ConnectionString()))
                {
                    command.Connection = connection;
                    connection.Open();
                    return command.ExecuteScalar();
                }
            }

            return null;
        }

        public static SqlCommand ExecuteScalar(SqlCommand command)
        {
            if (command != null)
            {
                using (SqlConnection connection = new SqlConnection(Pes.DBFactory.DBConnection.ConnectionString()))
                {
                    command.Connection = connection;
                    connection.Open();
                    command.ExecuteScalar();
                    return command;
                }
            }

            return null;
        }

        public static DataTable GetDataTable(SqlCommand command)
        {
            if (command != null)
            {
                using (SqlConnection connection = new SqlConnection(Pes.DBFactory.DBConnection.ConnectionString()))
                {
                    command.Connection = connection;
                    using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
                    {
                        using (DataTable dataTable = new DataTable())
                        {
                            dataTable.Locale = CultureInfo.InvariantCulture;
                            dataAdapter.Fill(dataTable);
                            return dataTable;
                        }
                    }
                }
            }

            return null;
        }

        public static SqlDataReader GetDataReader(SqlCommand command)
        {
            if (command != null)
            {
                SqlDataReader dataReader = default(SqlDataReader);
                using (SqlConnection connection = new SqlConnection(Pes.DBFactory.DBConnection.ConnectionString()))
                {
                    command.Connection = connection;
                    command.Connection.Open();
                    dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);
                    return dataReader;
                }
            }

            return null;
        }

        public static DataView GetDataView(SqlCommand command)
        {
            if (command != null)
            {
                using (DataView dataView = new DataView(GetDataTable(command)))
                {
                    return dataView;
                }
            }

            return null;
        }

        public static SqlDataAdapter GetDataAdapter(SqlCommand command)
        {
            if (command != null)
            {
                using (SqlConnection connection = new SqlConnection(Pes.DBFactory.DBConnection.ConnectionString()))
                {
                    command.Connection = connection;
                    using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
                    {
                        return dataAdapter;
                    }
                }
            }

            return null;
        }

        public static DataSet GetDataSet(SqlCommand command)
        {
            if (command != null)
            {
                using (SqlConnection connection = new SqlConnection(Pes.DBFactory.DBConnection.ConnectionString()))
                {
                    using (SqlDataAdapter dataAdapter = GetDataAdapter(command))
                    {
                        using (DataSet dataSet = new DataSet())
                        {
                            dataAdapter.Fill(dataSet);
                            dataSet.Locale = CultureInfo.CurrentUICulture;
                            return dataSet;
                        }
                    }
                }
            }

            return null;
        }
    }
}